﻿#region using

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

#endregion

namespace JupiterLibrary.UI
{
    /// <summary>
    /// Gestiona la Ventana del UI
    /// </summary>
    public class Window
    {
        Point clickPosition;

        bool resetClickPosition;

        bool movingWindow;

        public Rectangle WindowRectangle { get; private set; }
        public Rectangle CaptionRectangle { get; private set; }
        public Rectangle ClientRectangle { get; private set; }

        public int X;

        public int Y;

        public int Width;

        public int Height;

        public string Caption;

        public bool HasBorder;

        public Window(string caption, int x, int y, int width, int height)
        {
            Caption = caption;

            X = x;

            Y = y;

            Width = width;

            Height = height;

            HasBorder = true;

            resetClickPosition = true;

            movingWindow = false;
        }

        public void Update()
        {
            Point cursorPosition = Jupiter.Video.UI.Cursor.Position;

            if (movingWindow)
            {
                if (Jupiter.Input.IsLeftMouseButtonPressed())
                {
                    X += cursorPosition.X - clickPosition.X;
                    Y += cursorPosition.Y - clickPosition.Y;

                    clickPosition = cursorPosition;
                }
                else
                {
                    movingWindow = false;
                }
            }
            else
            {
                if (Jupiter.Input.IsLeftMouseButtonPressed(CaptionRectangle))
                {
                    // First time, so we store the position

                    clickPosition = cursorPosition;

                    movingWindow = true;

                }

            }

            WindowRectangle = new Rectangle(X, Y, Width, Height);
            CaptionRectangle = new Rectangle(X, Y, Width, 20);
        }

        public void Draw()
        {

            Jupiter.Video.Render.FilledRectangle(WindowRectangle, Jupiter.Video.UI.Prefs.WindowBackgroundColor);

            if(Caption!=null)
            {
                Jupiter.Video.Render.FilledRectangle(CaptionRectangle, Jupiter.Video.UI.Prefs.WindowTitleColor);
            }

            if (HasBorder)
            {
                Jupiter.Video.Render.Rectangle(WindowRectangle, Color.Black);
            }

        }

    }

    public class Windows
    {
        public List<Window> List;

        public Windows()
        {
            List = new List<Window>();
        }

        public void Update()
        {
            foreach (Window window in List)
            {
                window.Update();
            }
        }

        public void Draw()
        {
            foreach (Window window in List)
            {
                window.Draw();
            }
        }
    }
}
